package cn.lzm.org.service.impl;

import cn.lzm.basic.service.impl.BaseServiceImpl;
import cn.lzm.basic.util.MD5Utils;
import cn.lzm.basic.util.PageList;
import cn.lzm.basic.util.StrUtils;
import cn.lzm.org.domain.Department;
import cn.lzm.org.domain.Employee;
import cn.lzm.org.mapper.EmployeeMapper;
import cn.lzm.org.query.EmployeeQuery;
import cn.lzm.org.service.IEmployeeService;
import cn.lzm.user.domain.Logininfo;
import cn.lzm.user.mapper.LogininfoMapper;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findByDepartmentId(Long deptid) {
        return employeeMapper.findByDepartmentId(deptid);
    }

    @Override
    public void add(Employee employee) {
        // 对密码加密
        getEncrypByMd5(employee);
        // 给loginif赋初值
        Logininfo logininfo = initLogininfo(employee);
        // 将员工数据添加到logininfozhong
        logininfoMapper.add(logininfo);
        // 获得logininfo的主键id
        Long id = logininfo.getId();
        employee.setLogininfoId(id);
        employeeMapper.add(employee);

        // 添加员工时，会添加角色 ，所以需要对员工角色表进行修改
        // 添加角色时 为了方便后面修改时，不知道是增加还是减少  所以全部都使用向将全部数据删除  在将数据添加进去
        delEmpRole(employee);
        employeeMapper.addEmployeeRole(employee.getId(), employee.getRoles());
    }

    /**
     * 删除员工对应的角色
     * @param employee
     */
    private void delEmpRole(Employee employee) {
        employeeMapper.delEmployeeRoleByEmpId(employee.getId());
    }

    /**
     * 分页查询  将修改角色时回显 数据带上
     * @param query
     * @return
     */
    public PageList<Employee> queryByPage(EmployeeQuery query) {
        if (!StringUtils.isNullOrEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        Integer totals = employeeMapper.queryByCount(query);
        if (null == totals || 0 == totals) {
            return new PageList<>(0, new ArrayList<>());
        }
        // 查询数据
        List<Employee> employees = employeeMapper.queryByPage(query);
        for (Employee employee : employees) {
            Long id = employee.getId();
//            log.info(employeeMapper.findEmployeeRoleByEmpId(id).toString());
            List<Long> roleIds = employeeMapper.findEmployeeRoleByEmpId(id);
//            System.out.println(roleIds);
            if (roleIds != null) {
                for (Long roleId : roleIds) {
                    log.info(roleId.toString());


                    employee.getRoles().add(roleId) ;
                }
            }
        }
        //方到返回里面
        return new PageList<>(totals, employees);
    }

    @Override
    public void update(Employee employee) {
        // 根据前端传来的id或取对应的员工对象
        Employee emp = employeeMapper.findById(employee.getId());
        // 判断密码是不是一致，如果过一致  就不对密码进行加密
        if (employee.getPassword() != emp.getPassword()){
            getEncrypByMd5(employee);
        }
        // 初始化登录信息对象
        Logininfo logininfo = initLogininfo(employee);
        // 设置登录信息对象的id值
        logininfo.setId(employee.getLogininfoId());
        // 更新登录对象的信息
        logininfoMapper.update(logininfo);
        // 更新员工信息
        employeeMapper.update(employee);
        // 删除员工对象的角色信息
        delEmpRole(employee);
        // 根据员工id批量添加角色id
        employeeMapper.addEmployeeRole(employee.getId(), employee.getRoles());
    }

    @Override
    public void delete(Long id) {
        // 输出对应的logininfo表中的信息
        // 1.通过id查出对应logininfo_id
        Employee emp = employeeMapper.findById(id);
        Long logininfoId = emp.getLogininfoId();
        // 2.通过logininfoId去表中删除对数据
        logininfoMapper.delete(logininfoId);
        //通过id 到员工角色表中取删除相关数据
        employeeMapper.batchDelEmpRole(id);
        // 输出员工表中的数据
        employeeMapper.delete(id);
    }

    /**
     * 初始化化logininfo
     * @param employee
     */
    private Logininfo initLogininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setPassword(employee.getPassword());
        logininfo.setSalt(employee.getSalt());
        logininfo.setType(employee.getState());
        logininfo.setDisable(null);
        logininfo.setEmail(employee.getEmail());
        return logininfo;
    }

    /**
     * 给密码加密 得到盐值
     * @param employee
     */
    private void getEncrypByMd5(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String encrypByMd5 = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(encrypByMd5);
    }
}
